package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.system.controller.dto.AfterSaleCommitTask;
import cn.stylefeng.guns.modular.system.entity.AfterIndentCommit;
import cn.stylefeng.guns.modular.system.entity.AfterIndentCommitAudit;
import cn.stylefeng.guns.modular.system.entity.AfterIndentCommitCheck;
import cn.stylefeng.guns.modular.system.model.params.AfterIndentCommitAuditParam;
import cn.stylefeng.guns.modular.system.model.params.AfterIndentCommitCheckParam;
import cn.stylefeng.guns.modular.system.model.params.IndentOrderAuditParam;
import cn.stylefeng.guns.modular.system.service.AfterIndentCommitAuditService;
import cn.stylefeng.guns.modular.system.service.AfterIndentCommitCheckService;
import cn.stylefeng.guns.modular.system.service.AfterIndentCommitService;
import cn.stylefeng.guns.modular.system.service.IndentOrderService;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.guns.sys.core.util.common.SystemConstant;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 售后单控制器
 *
 * @author 小飞
 * @Date 2019-07-11 01:53:45
 */
@Controller
@RequestMapping("/afterIndentCommitCheck")
public class AfterIndentCommitCheckController extends BaseController {

    private String PREFIX = "/afterIndentCommitCheck";

    @Autowired
    private AfterIndentCommitCheckService afterIndentCommitCheckService;
    @Resource
    private TaskService taskService;
    @Autowired
    private AfterIndentCommitService afterIndentCommitService;
    @Autowired
    private UserService userService;
    @Autowired
    private IndentOrderService indentOrderService;
    @Autowired
    private AfterIndentCommitAuditService afterIndentCommitAuditService;
    @Autowired
    private DeptService deptService;

    /**
     * 跳转到主页面
     *
     * @author 小飞
     * @Date 2019-07-11
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/afterIndentCommitCheck.html";
    }

    /**
     * 新增页面
     *
     * @author 小飞
     * @Date 2019-07-11
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/afterIndentCommitCheck_add.html";
    }

    /**
     * 编辑页面
     *
     * @author 小飞
     * @Date 2019-07-11
     */
    @RequestMapping("/edit")
    public String edit(Long id,String taskId,Long saleId) {
        super.setAttr("id",id);
        super.setAttr("saleId",saleId);
        super.setAttr("taskId",taskId);
        super.setAttr("username",userService.getById(ShiroKit.getUserNotNull().getId()).getName());
        super.setAttr("deptName",deptService.getById(ShiroKit.getUserNotNull().getDeptId()).getSimpleName());
        return PREFIX + "/afterIndentCommitCheck_edit.html";
    }

    /**
     * 新增接口
     *
     * @author 小飞
     * @Date 2019-07-11
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(AfterIndentCommitCheckParam afterIndentCommitCheckParam) {
        this.afterIndentCommitCheckService.add(afterIndentCommitCheckParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author 小飞
     * @Date 2019-07-11
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(AfterIndentCommitCheckParam afterIndentCommitCheckParam) {
        this.afterIndentCommitCheckService.update(afterIndentCommitCheckParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author 小飞
     * @Date 2019-07-11
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(AfterIndentCommitCheckParam afterIndentCommitCheckParam) {
        this.afterIndentCommitCheckService.delete(afterIndentCommitCheckParam);
        return ResponseData.success();
    }


    @RequestMapping("/shenheIndex")
    public String shenheIndex(Long id,String taskId){
        super.setAttr("id",id);
        super.setAttr("taskId",taskId);
        return  PREFIX+ "/check.html";
    }
    /**
     * 查看详情接口
     *
     * @author 小飞
     * @Date 2019-07-11
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(AfterIndentCommitCheckParam afterIndentCommitCheckParam) {
        AfterIndentCommitCheck detail = this.afterIndentCommitCheckService.getById(afterIndentCommitCheckParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author 小飞
     * @Date 2019-07-11
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list() {

        List<Task> tasks = taskService.createTaskQuery().processDefinitionKey(SystemConstant.afterSale).taskCandidateUser(ShiroKit.getUserNotNull().getId() + "").list();

        if (tasks.size() == 0){
            return  new LayuiPageInfo();
        }

        List<AfterSaleCommitTask> afterSaleCommitTasks = tasks.stream().filter(task -> afterIndentCommitService.getOne(new QueryWrapper<AfterIndentCommit>()
                .lambda().eq(AfterIndentCommit::getProessInstanceId,task.getProcessInstanceId())) !=null).map(task -> {

            AfterSaleCommitTask afterSaleCommitTask = new AfterSaleCommitTask();
            AfterIndentCommit afterIndentCommit = afterIndentCommitService.getOne(new QueryWrapper<AfterIndentCommit>()
                    .lambda().eq(AfterIndentCommit::getProessInstanceId,task.getProcessInstanceId()));

            BeanUtils.copyProperties(afterIndentCommit,afterSaleCommitTask);
            afterSaleCommitTask.setTypeStr(afterIndentCommit.getType() == 1 ? "维修":"售后");
            afterSaleCommitTask.setProcessInstanceId(task.getProcessInstanceId());
            afterSaleCommitTask.setTaskId(task.getId());
            afterSaleCommitTask.setDealerName(userService.getById(indentOrderService.getById(afterIndentCommit.getIndentOrderId()).getDealer()).getName());
            return  afterSaleCommitTask;
        }).collect(Collectors.toList());

        LayuiPageInfo layuiPageInfo = new LayuiPageInfo();
        layuiPageInfo.setCount(afterSaleCommitTasks.size());
        layuiPageInfo.setData(afterSaleCommitTasks);
        return  layuiPageInfo;
    }

    /**
     * 审核
     * @param afterIndentCommitAuditParam
     * @return
     */
    @ResponseBody
    @RequestMapping("/check")
    public ResponseData check(AfterIndentCommitAuditParam afterIndentCommitAuditParam){

        AfterIndentCommitAudit afterIndentCommitAudit = new AfterIndentCommitAudit();
        BeanUtils.copyProperties(afterIndentCommitAuditParam,afterIndentCommitAudit);
        afterIndentCommitAudit.setAfterIndentCommitId(afterIndentCommitAuditParam.getAfterSaleCommitId());
        Task task = taskService.createTaskQuery().taskId(afterIndentCommitAuditParam.getTaskId()).singleResult();
        afterIndentCommitAudit.setProcessInstanceId(task.getProcessInstanceId());
        afterIndentCommitAudit.setTaskId(task.getId());
        afterIndentCommitAudit.setAuditTime(new Date());
        afterIndentCommitAudit.setUserId(ShiroKit.getUserNotNull().getId());
        afterIndentCommitAuditService.save(afterIndentCommitAudit);

        Map<String,Object> map = new HashMap<>();
        map.put("status",afterIndentCommitAuditParam.getAuditStatus());
        taskService.complete(task.getId(),map);

        return  ResponseData.success();
    }




}


